427C - Checkposts - CodeForces Solution


dfs and similar graphs two pointers *1700

Please click on ads to support us..

Python Code:

n, m = int(input()) + 1, 1000000007
q, p = [[] for i in range(n)], [[] for i in range(n)]
w = [0] + list(map(int, input().split()))
for i in range(int(input())):
    u, v = map(int, input().split())
    p[u].append(v)
    q[v].append(u)
r = set(i for i in range(1, n) if not p[i] or not q[i])
s, t = 0, 1
while r:
    i = r.pop()
    s += w[i]
    for j in p[i]:
        q[j].remove(i)
        if not q[j]: r.add(j)
    for j in q[i]:
        p[j].remove(i)
        if not p[j]: r.add(j)
r = set(i for i in range(1, n) if p[i] and q[i])
while r:
    i = r.pop()
    h = p[i]
    d, k = w[i], 1
    while h:
        i = h.pop()
        if not i in r: continue
        r.remove(i)
        h += p[i]
        if w[i] == d: k += 1
        elif w[i] < d: d, k = w[i], 1
    s += d
    t = (t * k) % m
print(s,t)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define int long long int
const int mod=1e9+7;
map<int,int> mp;
void dfs(int node,vector<int> &vis,vector<int> g[],stack<int> &st){
    vis[node]=1;
    for(auto it:g[node]){
        if(vis[it])continue;
        dfs(it,vis,g,st);
    }
    st.push(node);
}


void Dfs(int val,vector<int> &vis,vector<int> g[],int &mini,vector<int> &c){
    vis[val]=1;
    mp[c[val]]++;
    mini=min(mini,c[val]);
    for(auto it:g[val]){
        if(vis[it])continue;
        Dfs(it,vis,g,mini,c);
    }
}

void Solve(){
   int n;
   cin>>n;

   vector<int> cost(n+1);

   for(int i=1;i<=n;i++)cin>>cost[i];

   int m;
   cin>>m;

   vector<int> g[n+1],rev[n+1];

   for(int i=1;i<=m;i++){
    int u,v;
    cin>>u>>v;
    g[u].push_back(v);
    rev[v].push_back(u);
   }

   stack<int> st;
   vector<int> vis1(n+1,0);
  for(int i=1;i<=n;i++){
    if(!vis1[i]){
        dfs(i,vis1,g,st);
    }
}
   vector<int> vis(n+1,0);
   int ans=0,res=1;
   while(st.size()>0){
    int val=st.top();
    // cout<<val<<endl;
    st.pop();
    if(vis[val])continue;
    mp.clear();
    int mini=9e9;
    Dfs(val,vis,rev,mini,cost);
    ans+=mini;
    res*=mp[mini];
    res=res%mod;
   }
   cout<<ans<<" "<<res<<endl;
}



int32_t main(){
    int T=1;
    // cin>>T;
    while(T--){
        Solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game